package com.sali.回溯算法;

import java.util.ArrayList;
import java.util.List;

/**
 * 组合
 */
public class LeetCode_77 {


    public static void main(String[] args) {
        int n = 4;
        int k = 2;
        List<List<Integer>> combine = combine(n, k);
        for (List<Integer> list : combine) {
            for (Integer en : list) {
                System.out.print(en + " ");
            }
            System.out.println();
        }
    }


    public static List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> res = new ArrayList<>();
        getRes(n, k, new ArrayList<>(), res, 1);
        return res;
    }

    private static void getRes(int n, int k, List<Integer> path, List<List<Integer>> res, int startPoint) {
        if (path.size() == k) {
            res.add(new ArrayList<>(path));
            return;
        }

        for (int i = startPoint; i <= n; i++) {
            path.add(i);
            getRes(n, k, path, res, i + 1);
            path.remove(path.size() - 1);
        }
    }

}
